package com.iteaj.network.codec.adapter;

import com.iteaj.network.AbstractMessage;
import com.iteaj.network.codec.IotMessageDecoder;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.FixedLengthFrameDecoder;

/**
 * create time: 2021/8/13
 *  固定字段解码适配器
 * @author iteaj
 * @since 1.0
 */
public class FixedLengthFrameDecoderAdapter extends FixedLengthFrameDecoder implements IotMessageDecoder {

    private Class<?> messageClass;

    /**
     * Creates a new instance.
     *
     * @param frameLength the length of the frame
     */
    public FixedLengthFrameDecoderAdapter(int frameLength) {
        super(frameLength);
    }


    @Override
    protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
        final Object decode = super.decode(ctx, in);
        return this.proxy(ctx, (ByteBuf) decode);
    }

    @Override
    public Class<? extends AbstractMessage> getMessageClass() {
        return (Class<? extends AbstractMessage>) this.messageClass;
    }

    @Override
    public IotMessageDecoder setMessageClass(Class<? extends AbstractMessage> messageClass) {
        this.messageClass = messageClass;
        return this;
    }
}
